<template>
  <div class="login-setting">
    <el-form
      ref="userForm"
      :rules="rules"
      :model="userInfo"
      label-width="120px"
      style="margin-left: 120px; margin-top: 30px"
    >
      <el-form-item label="姓名:" prop="username">
        <el-input v-model="userInfo.username" style="width: 300px" />
      </el-form-item>
      <el-form-item label="密码:" prop="password">
        <el-input
          v-model="userInfo.password"
          style="width: 300px"
          type="password"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="hSave">更新</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>
<script>
import { getInfoById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
export default {
  data() {
    return {
      // userId: this.$route.query.id,
      userId: this.$route.params.id,
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loadInfoById()
  },
  methods: {
    // ...
    hSave() {
      this.$refs.userForm.validate(valid => {
        if (!valid) return
        this.updateUserDetailById()
      })
    },
    // 获取登录信息
    async loadInfoById() {
      try {
        const res = await getInfoById(this.userId)
        console.log('getInfoById', res)
        this.userInfo = { ...res.data, password: '' }
      } catch (error) {
        console.log(error)
      }
    },
    // 更新员工登录信息
    async updateUserDetailById() {
      try {
        const res = await saveUserDetailById(this.userInfo)
        console.log('saveUserDetailById', res)
        this.$router.back()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
